package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.annotation.RequireLogin;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVOService;
import cn.wolfcode.luowowo.cache.service.ITravelStatisVOService;
import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.domain.UserInfo;
import cn.wolfcode.luowowo.service.IUserInfoService;
import cn.wolfcode.luowowo.util.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("users")
@Api(value = "用户资源",description = "用户资源控制器")
public class UserInfoController {

    @Value("${sms.url}")
    private String smsUrl;
    @Value("${sms.appkey}")
    private String appkey;
    @Value("${sms.sign}")
    private String smsSign;

    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IUserInfoRedisService userInfoRedisService;

    @Autowired
    private IStrategyStatisVOService strategyStatisVOService;

    @Autowired
    private ITravelStatisVOService travelStatisVOService;

    @GetMapping("/list")
    public Object list(){
        UserInfo userInfo = userInfoService.findById("5e295f01a00e265228f963ea");
        return userInfo;
    }

    //得到当前用户身份信息
    @RequireLogin
    @GetMapping("/currentUser")
    public Object currentUser(HttpServletRequest request){
        String token = request.getHeader("token");
        UserInfo  userInfo=userInfoRedisService.getUserByToken(token);
        return JsonResult.success(userInfo);
    }

    //检查用户手机号是否注册过
    @GetMapping("/checkPhone")
    public boolean checkPhone(String phone){
        boolean b=userInfoService.checkPhone(phone);
        return !b;
    }

    //发送验证码
    @GetMapping("/sendVerifyCode")
    public Object sendVerifyCode(String phone){
        userInfoService.sendVerifyCode(phone, smsUrl, appkey,smsSign);
        return JsonResult.success();
    }

    //注册
    @ApiOperation(value = "注册功能",notes = "其实就是新增用户")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "手机",name = "phone",dataType = "String",required = true),
            @ApiImplicitParam(value = "昵称",name = "nickname",dataType = "String",required = true),
            @ApiImplicitParam(value = "密码",name = "password",dataType = "String",required = true),
            @ApiImplicitParam(value = "确认密码",name = "rpassword",dataType = "String",required = true),
            @ApiImplicitParam(value = "验证码",name = "verifyCode",dataType = "String",required = true)
    })
    @PostMapping("/regist")
    public Object regist(String phone,String nickname,String password,String rpassword,String verifyCode){
           userInfoService.regist(phone,nickname,password,rpassword,verifyCode);
           return JsonResult.success();
    }

    //登录
    @PostMapping("/login")
    public Object login(String username,String password){
        //执行登录操作
        UserInfo userInfo= userInfoService.login(username,password);
        String token = UUID.randomUUID().toString().replaceAll("-", "");
        userInfoRedisService.setToken(token,userInfo);
        //创建map，封装数据到前端页面
        HashMap<String, Object> map = new HashMap<>();
        map.put("token",token);
        map.put("user",userInfo);
        return JsonResult.success(map);
    }

    //返回当前用户拥有攻略id集合数据
    @GetMapping("/strategies/favor")
    public Object favor(String sid,String userId){
        List<String> list=strategyStatisVOService.getFavorStrategyIds(sid,userId);
        return  JsonResult.success(list);
    }
    //返回当前用户拥有游记id集合数据
    @GetMapping("/travels/favor")
    public Object travelfavor(String tid,String userId){
        List<String> list=travelStatisVOService.getFavorTravelIds(tid,userId);
        return  JsonResult.success(list);
    }
}
